package request

import (
	"reflect"
	"regexp"

	"github.com/go-playground/locales/en"
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	zht "github.com/go-playground/validator/v10/translations/zh"
)

func init() {
}

// Validate format validator
func Validate(obj interface{}) ([]string, error) {

	enl := en.New()
	zhl := zh.New()

	v := validator.New()
	uni := ut.New(zhl, zhl, enl)

	translator, _ := uni.GetTranslator("zh")

	if err := zht.RegisterDefaultTranslations(v, translator); err != nil {
		return nil, err
	}

	//获取 comment 作为翻译字段
	zht.RegisterDefaultTranslations(v, translator)
	v.RegisterTagNameFunc(func(fld reflect.StructField) string {
		return fld.Tag.Get("comment")
	})

	_ = v.RegisterValidation("unique", uniqueRule)
	_ = v.RegisterTranslation("unique", translator, func(ut ut.Translator) error {
		return ut.Add("unique", "{0} {1} 已存在!", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("unique", fe.Field(), fe.Value().(string))
		return t
	})

	//检查手机号是否符合规则
	_ = v.RegisterValidation("checkPhoneRex", checkPhoneRex)
	_ = v.RegisterTranslation("checkPhoneRex", translator, func(ut ut.Translator) error {
		return ut.Add("checkPhoneRex", "{0} 不符合规则", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("checkPhoneRex", fe.Field(), fe.Value().(string))
		return t
	})

	//检查身份证号是否符合规则
	_ = v.RegisterValidation("checkIdentityRex", checkIdentityRex)
	_ = v.RegisterTranslation("checkIdentityRex", translator, func(ut ut.Translator) error {
		return ut.Add("checkIdentityRex", "{0} 不符合规则", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("checkIdentityRex", fe.Field(), fe.Value().(string))
		return t
	})

	//检查身份证号是否符合规则
	_ = v.RegisterValidation("checkSocCodeRex", checkSocCodeRex)
	_ = v.RegisterTranslation("checkSocCodeRex", translator, func(ut ut.Translator) error {
		return ut.Add("checkSocCodeRex", "{0} 不符合规则", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("checkSocCodeRex", fe.Field(), fe.Value().(string))
		return t
	})

	errors := make([]string, 0)

	err := v.Struct(obj)
	if err != nil {
		if errs, ok := err.(validator.ValidationErrors); ok {
			for _, err := range errs {
				errors = append(errors, err.Translate(translator))
			}
		}

		return errors, err
	}

	return nil, nil
}

func uniqueRule(fl validator.FieldLevel) bool {

	funcMap := make(map[string]func(interface{}) bool)
	//funcMap["username"] = uniqueNickname
	//funcMap["phone"] = uniquePhone

	validateFunc := fl.Param()

	return uniqueValidateFunc(fl.Field().String(), funcMap[validateFunc])
}

func checkPhoneRex(fl validator.FieldLevel) bool {
	// 获取手机号
	phone := fl.Field().String()
	if phone == "" {
		return true
	}
	regular := "^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(phone)
}

func uniqueValidateFunc(val interface{}, f func(interface{}) bool) bool {
	return f(val)
}

// checkIdentityRex 校验身份证号
func checkIdentityRex(fl validator.FieldLevel) bool {
	identity := fl.Field().String()
	reg := regexp.MustCompile(`(^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)`)
	return reg.MatchString(identity)
}

// checkSocCodeRex 检验统一社会信用代码格式
func checkSocCodeRex(fl validator.FieldLevel) bool {
	socCode := fl.Field().String()
	reg := regexp.MustCompile(`[1-9A-GY]{1}[1239]{1}[1-5]{1}[0-9]{5}[0-9A-Z]{10}$`)

	return reg.MatchString(socCode)
}

/*func uniqueNickname(username interface{}) bool {

	user := model.GetUserByName(username.(string))

	return user == nil
}*/

/*func uniquePhone(phone interface{}) bool {

	user := model.GetUserByPhone(phone.(string))

	return user == nil
}*/
